<?php

namespace App\Api\V1\Controllers;

use Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;

class AuthorizationController extends Controller
{
    protected $guard = 'api';

    /**
     * 注册用户
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function signUp(Request $request)
    {
        $this->validate($request, [
            'name'      => 'required',
            'telephone' => 'required',
            'password'  => 'required',
        ]);

        $user = new User($request->all());

        if (!$user->save()) {
            throw new HttpException(500);
        }
        $token = Auth::guard($this->guard)->login($user);

        return response()->json([
            'status' => 'ok',
            'token' => $token,
        ], 201);
    }

    /**
     * 根据用户名密码 获取token
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function token(Request $request)
    {
        $credentials = [
            'telephone' => $request->telephone,
            'password'  => $request->password
        ];
        try {
            if (!$token = Auth::guard($this->guard)->attempt($credentials)) {
                return response()->json(['error' => 'invalid_credentials'], 401);
            }
        } catch (JWTException $e) {
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        return response()->json(compact('token'));
    }

    /**
     * 刷新token
     *
     * @return mixed
     */
    public function refreshToken()
    {
        $token = Auth::guard($this->guard)->refresh();

        return $this->response->array(compact('token'));
    }

    /**
     * 个人信息
     *
     * @return User|null
     */
    public function me()
    {
        return Auth::guard('api')->user();
    }

    /**
     * 退出
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout()
    {
        Auth::guard($this->guard)->logout();

        return response()->json(['status' => 'ok']);
    }

    /**
     * 登录
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(Request $request)
    {
        $telephone = $request->telephone;
        $user = User::where('telephone', $telephone)->first();
        $token = Auth::guard($this->guard)->login($user);

        return response()->json([
            'status' => 'ok',
            'token' => $token,
        ], 201);
    }

}
